-module(sub_traffic).
-include("protocol.hrl").
-include("subscriber.hrl").
-export([analysis/2]).
analysis(#sub_packet{dir=Dir,prot=16#800,packet=Packet},Sub) ->
	case Packet of
	<<4:4,_HdrLen:4,_Tos:8,Len:16,
	  _Id:16,_Flags:3,_FragOffset:13,
	  _TTL:8,_Protocol:8,_CheckSum:16,
	  _SIP:4/binary,
	  DIP:4/binary,
	  _Payload/bytes>> when Dir =:= down -> %% 
 		if 
			DIP =/= <<0,0,0,0>> ->
				Sub#subscriber{
						ip= DIP,
						total_p_down = Sub#subscriber.total_p_down + 1,
						total_b_down = Sub#subscriber.total_b_down + Len};
			true ->
				Sub
		end;
	<<4:4,_HdrLen:4,_Tos:8,Len:16,
	  _Id:16,_Flags:3,_FragOffset:13,
	  _TTL:8,_Protocol:8,_CheckSum:16,
	  _SIP:4/binary,
	  _DIP:4/binary,
	  _Payload/bytes>> when Dir =:= up -> %% 
		Sub#subscriber{
			total_p_down = Sub#subscriber.total_p_down + 1,
			total_b_down = Sub#subscriber.total_b_down + Len};
	_ ->
	  error_logger:info_report([{unrecognized_sub_packet,Packet},{reason,"unknown ip protocol"}]),
	  Sub
	end;
analysis(Sub_Packet,Sub) ->
	error_logger:info_report([{unrecognized_sub_packet,Sub_Packet},{reason,"unknow layer3 protocol"}]),
	Sub.
	
	  